package com.ecoolex.ying.space.core.manager;

import java.sql.Date;
import java.time.Instant;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ecoolex.framework.common.util.Check;
import com.ecoolex.ying.space.core.dao.UserWorksVideoDAO;
import com.ecoolex.ying.space.entity.UserWorks;
import com.ecoolex.ying.space.entity.UserWorksVideo;
import com.ecoolex.ying.space.entity.UserWorksVideo.Column;
import com.ecoolex.ying.space.pojo.AddUserWorksVideoDTO;
import com.ecoolex.ying.space.pojo.EditUserWorksVideoDTO;


@Service
public class UserWorksVideoManager {

    @Autowired
    private UserWorksVideoDAO dao;

    @Autowired
    private UserWorksShoppingManager userWorksShoppingManager;

    public UserWorksVideo addUserWorksVideo(UserWorks userWorks, AddUserWorksVideoDTO addUserWorksVideoDTO, Integer userId, String createUser) {
        UserWorksVideo userWorksVideo = new UserWorksVideo();
        userWorksVideo.setUserId(userWorks.getUserId());
        userWorksVideo.setWorksId(userWorks.getWorksId());
        userWorksVideo.setVideo(addUserWorksVideoDTO.getStorageId());
        userWorksVideo.setContent(addUserWorksVideoDTO.getContent());
        userWorksVideo.setDeleted(UserWorksVideo.NOT_DELETED);
        userWorksVideo.setCreateUser(createUser);
        userWorksVideo.setCreateTime(Date.from(Instant.now()));
        userWorksVideo.setUpdateUser(createUser);
        userWorksVideo.setUpdateTime(Date.from(Instant.now()));

        dao.insert(userWorksVideo);
        return userWorksVideo;
    }

    public void editUserWorksVideo(UserWorks userWorks, EditUserWorksVideoDTO editUserWorksVideoDTO, String updateUser) {
        // UserWorksVideo userWorksVideo = getByWorksId(editUserWorksVideoDTO.getWorksId());
        // BizAssert.isTrue(Check.notNull(userWorksVideo),ResultCode.VERIFY.build(10), "视频作品不存在");
        // userWorksVideo.setContent(editUserWorksVideoDTO.getContent());
        // userWorksVideo.setVideo(editUserWorksVideoDTO.getStorageId());
        // userWorksVideo.setUpdateUser(updateUser);
        // userWorksVideo.setUpdateTime(Date.from(Instant.now()));
        //
        // dao.updateByPrimaryKeySelective(userWorksVideo,
        // Column.excludes(Column.deleted,Column.createUser,Column.createTime));
        // //处理作品购物
        // List<AddUserWorksShoppingsDTO> userWorksShoppingsDTOList =
        // editUserWorksVideoDTO.getAddUserWorksShoppingsDTOList();
        // userWorksShoppingManager.updateUserWorksShopping(userWorks, editUserWorksVideoDTO.getStorageId(),
        // userWorksShoppingsDTOList, updateUser);
    }

    public void delete(UserWorks userWorks, String updateUser) {
        UserWorksVideo userWorksVideo = getByWorksId(userWorks.getWorksId());
        if (Check.notNull(userWorksVideo)) {
            userWorksVideo.setDeleted(UserWorksVideo.IS_DELETED);
            userWorksVideo.setUpdateUser(updateUser);
            userWorksVideo.setUpdateTime(Date.from(Instant.now()));
            dao.updateByPrimaryKeySelective(userWorksVideo, Column.deleted, Column.updateUser, Column.updateTime);
            userWorksShoppingManager.deleteByWorksId(userWorks.getWorksId(), updateUser);
        }
    }

    public UserWorksVideo getByWorksId(Integer worksId) {
        return dao.selectByPrimaryKeyWithLogicalDelete(worksId, UserWorksVideo.NOT_DELETED);
    }
}